package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("order")
public class OrderMobileController {

    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    /**
     * 新增
     * @param map
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody Map map){

        try {
            //用户提交的令牌
            String token = (String) map.get("token");
            //获取redis中的令牌
            String redisToken = jedisPool.getResource().get(token);
            if (redisToken==null || !token.equals(redisToken)){
                return new Result(false,"表单不能重复提交");
            }
            //删除redis中的令牌
            jedisPool.getResource().del(token);

            String telephone = (String) map.get("telephone");

            //设置key
            String key = telephone+ RedisMessageConstant.SENDTYPE_ORDER;
            //获取redis中的验证码
            String redisCode = jedisPool.getResource().get(key);

            String validateCode = (String) map.get("validateCode");

            //校验验证码
            if (redisCode==null || !redisCode.equals(validateCode)){
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }else{
                //执行新增预约订单
                Order order = orderService.add(map);

                //发送短信通知，通知用户预约成功
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,telephone, ValidateCodeUtils.generateValidateCode(4).toString());

                return new Result(true,MessageConstant.ADD_ORDER_SUCCESS,order);
            }
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ORDER_FAIL);
        }

    }

    /**
     * 生成令牌
     */
    @RequestMapping("token")
    public Result token(){

        String token = UUID.randomUUID().toString();

        //将生成的令牌保存到redis中并设置一个10分钟的有效时间
        jedisPool.getResource().setex(token,60*10,token);

        //将生成的令牌响应到前端
        return new Result(true,"",token);

    }

    /**
     * 回显订单信息
     */
    @RequestMapping("findById")
    public Result findById(@RequestParam("id")Integer id){

        try {
            Map map = orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }

    }
}
